package com.lanou.dllo.findfood.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.dllo.findfood.activity.FindBeautifulActivity;
import com.lanou.dllo.findfood.activity.FindFoodNewsActivity;
import com.lanou.dllo.findfood.activity.FindHealthActivtiy;
import com.lanou.dllo.findfood.activity.FindLifeActivity;
import com.lanou.dllo.findfood.activity.FindOtherActivity;
import com.lanou.dllo.findfood.activity.FindPregnancyActivity;
import com.lanou.dllo.findfood.activity.FindSocialhotAciticvity;
import com.lanou.dllo.findfood.adpter.FindViewperApter;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.database.FindViewPageDataUtil;
import com.lanou.dllo.findfood.entity.FindTopAll;
import com.lanou.dllo.findfood.entity.FindTopResult;
import com.lanou.dllo.findfood.volley.VolleySington;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by dllo on 15/10/20.
 */
public class FindFragment extends Fragment implements View.OnClickListener {
    private ViewPager viewPager;
    private FindViewperApter perAdpter;
    private Handler handler;
    private FindViewPageDataUtil dataUtil;

    private TextView socialhot,lifeTv,Foodconsulting,beautiful,health,Pregnancy,Other;

    private int maxItem = 0;//最大下标
    private int currentItem = 0;//当前图片的下标
    private Runnable r = new Runnable() {
        //变换下标的过程
        //递归
        @Override
        public void run() {
            //设置当前左右组件的下标为1  即第二张图片

            currentItem++;
            maxItem = perAdpter.getCount();//获取广告栏图片的下标数
            viewPager.setCurrentItem(currentItem % maxItem);
            handler.postDelayed(r, 2000);

        }
    };


    @Override
    public void onAttach(Context context) {

        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_find, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
      //  initData();

       List<FindTopResult> results = dataUtil.queryAll();
        if (results ==null || results.size()==0){
            initData();

        }
        else {
            perAdpter.add(results);
            handler.postDelayed(r, 2000);
        }


    }


    private void initView() {
        viewPager = (ViewPager) getActivity().findViewById(R.id.findViewPage);
        perAdpter = new FindViewperApter(getFragmentManager());
        viewPager.setAdapter(perAdpter);
        handler = new Handler();
        dataUtil = new FindViewPageDataUtil(getActivity());



        socialhot = (TextView) getActivity().findViewById(R.id.socialhot);
        socialhot.setOnClickListener(this);


        lifeTv = (TextView) getActivity().findViewById(R.id.lifeTv);
        lifeTv.setOnClickListener(this);
        Foodconsulting = (TextView) getActivity().findViewById(R.id.Foodconsulting);
        Foodconsulting.setOnClickListener(this);
        beautiful = (TextView) getActivity().findViewById(R.id.beautiful);
        beautiful.setOnClickListener(this);
        health = (TextView) getActivity().findViewById(R.id.health);
        health.setOnClickListener(this);
        Pregnancy = (TextView) getActivity().findViewById(R.id.Pregnancy);
        Pregnancy.setOnClickListener(this);
        Other = (TextView) getActivity().findViewById(R.id.Other);
        Other.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.socialhot:
                Intent intent = new Intent(getActivity(),FindSocialhotAciticvity.class);
                startActivity(intent);

            break;
            case R.id.lifeTv:
                Intent intent2 = new Intent(getActivity(),FindLifeActivity.class);
                startActivity(intent2);

                break;
            case R.id.Foodconsulting:
                Intent intent3 = new Intent(getActivity(),FindFoodNewsActivity.class);

                startActivity(intent3);
                break;
            case R.id.beautiful:
                Intent intent4 = new Intent(getActivity(), FindBeautifulActivity.class);
                startActivity(intent4);

                break;
            case R.id.health:
                Intent intent5 = new Intent(getActivity(), FindHealthActivtiy.class);
                startActivity(intent5);


                break;
            case R.id.Pregnancy:
                Intent intent6 = new Intent(getActivity(), FindPregnancyActivity.class);
                startActivity(intent6);


                break;
            case R.id.Other:
                Intent intent7 = new Intent(getActivity(), FindOtherActivity.class);
                startActivity(intent7);
                break;

            default:
                break;

        }

    }

    //数据获得 广告栏的部分
    private void initData() {
        String httpUrl = "http://apis.baidu.com/tngou/lore/list";
        String httpArg = "id=0&page=1&rows=5";
        String path = httpUrl + "?" + httpArg;
        StringRequest request = new StringRequest(path, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                FindTopAll all = gson.fromJson(response, FindTopAll.class);
                if (all != null) {
                    List<FindTopResult> result = all.getTngou();

                    perAdpter.add(result);
                    dataUtil.deleData();
                    dataUtil.saveAll(result);
                    handler.postDelayed(r, 2000);
                }
                //


            }


        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                error.printStackTrace();
                ;
            }
        }

        ) {
            @Override
            //在StringRespset结束之前写{}在大括号里面写 getHeaders() 自动重写方法
            //用Map方法键值对形势添加
            //添加请求头
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                map.put("apikey", "7df89e2ed7c7b92e1a2fe5ec8036d0cd");
                return map;
            }
        };

        VolleySington.getInstance().addRequest(request);


    }


}
